t = int(input())
while t:
a, b = list(map(int, input().split(' ')))
count = b - a
for i in range(b): if b == b | i:
count = min(count, 1 + abs(i - a))
print(count)
t -= 1
//************ || MD. SAJID ALAM CHOWDHURY || *************//
//**************** || 25-Mar-2023(Sat) || *****************//
//******************** || 18:38:03 || *********************//
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<functional>
using namespace std;
using namespace __gnu_pbds;
//********************* || DEFINES || *********************//
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ff first
#define ss second
#define IN insert
#define MP make_pair
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define NP next_permutation
#define PP prev_permutation
#define all(v) v.begin(),v.end()
#define Fo(i,a,b) for(ll i=a;i<=b;i++)
#define Fb(i,b,a) for(ll i=b;i>=a;i--)
#define mem_0(arr) memset(arr,0,sizeof(arr))
#define mem_1(arr) memset(arr,-1,sizeof(arr))
#define precise_impact(x) fixed<<showpoint<<setprecision(x)
//******************** || TYPEDEFS || *********************//
typedef double db;
typedef long long ll;
typedef long double ldb;
typedef unsigned long long ull;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pref_trie;
//******************** || CONSTANTS || ********************//
const ll MAX=2e5+5;
const ll MOD=1e9+7; // 998244353 //
const ll INF=1e18+5;
const db PI=acos(-1);
//--------- Up-Down,Left-Right ---------//
//const ll dx[]={ 0, 0,-1, 1};
//const ll dy[]={-1, 1, 0, 0};
//------------ King's Moves ------------//
//const ll dx[]={ 0, 0,-1,-1,-1, 1, 1, 1};
//const ll dy[]={-1, 1, 0,-1, 1, 0,-1, 1};
//----------- Knight's Moves -----------//
//const ll dx[]={-2,-2,-1,-1, 1, 1, 2, 2};
//const ll dy[]={-1, 1,-2, 2,-2, 2,-1, 1};
//***************** || INLINE FUNCTIONS || ****************//
inline ll norm(ll &a) { a%=MOD; return (a<0)?(a+=MOD):a; }
inline ll modAdd(ll a,ll b) { a%=MOD,b%=MOD; norm(a),norm(b); a+=b; return norm(a); }
inline ll modSub(ll a,ll b) { a%=MOD,b%=MOD; norm(a),norm(b); a-=b; return norm(a); }
inline ll modMul(ll a,ll b) { a%=MOD,b%=MOD; norm(a),norm(b); a*=b; return norm(a); }
inline ll modPow(ll a,ll p) { ll c=1; while(p){ if(p%2){ c=modMul(c,a); } a=modMul(a,a); p/=2; } return c; }
inline ll modInverse( ll a) { return modPow(a,MOD-2); }
inline ll modDiv(ll a,ll b) { a%=MOD,b%=MOD; norm(a),norm(b); return modMul(a,modInverse(b)); }
//******************** || FUNCTIONS || ********************//
ll gcd(ll m,ll n)
{
while(n!=0)
{
m%=n;
swap(m,n);
}
return m;
}
ll lcm(ll m,ll n)
{
return ((m*n)/gcd(m,n));
}
ll bit_on(ll pos,ll mask)
{
return mask|(1<<pos);
}
bool bit_check(ll pos,ll mask)
{
return mask&(1<<pos);
}
bool cmp(pair<ll,ll>a,pair<ll,ll>b)
{
if(a.ff==b.ff)
{
return (a.ss>b.ss);
}
return (a.ff<b.ff);
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
/***********************/ BOOST /***********************/
//***************** || READY TO GO || *****************//
ll t;
cin>>t;
while(t--)
{
ll a,b;
cin>>a>>b;
ll ans=INF;
Fo(i,0,b)
{
if((i|b)==b)
{
ll k=abs(i-a);
if(i!=b)
{
k++;
}
ans=min(ans,k);
}
}
cout<<ans<<endl;
}
return 0;
//*************** || DONE AND DUSTED || ***************//
}
1032A - Kitchen Utensils | 1501B - Napoleon Cake |
1584B - Coloring Rectangles | 1562B - Scenes From a Memory |
1521A - Nastia and Nearly Good Numbers | 208. Implement Trie |
1605B - Reverse Sort | 1607C - Minimum Extraction |
1604B - XOR Specia-LIS-t | 1606B - Update Files |
1598B - Groups | 1602B - Divine Array |
1594B - Special Numbers | 1614A - Divan and a Store |
2085. Count Common Words With One Occurrence | 2089. Find Target Indices After Sorting Array |
2090. K Radius Subarray Averages | 2091. Removing Minimum and Maximum From Array |
6. Zigzag Conversion | 1612B - Special Permutation |
1481. Least Number of Unique Integers after K Removals | 1035. Uncrossed Lines |
328. Odd Even Linked List | 1219. Path with Maximum Gold |
1268. Search Suggestions System | 841. Keys and Rooms |
152. Maximum Product Subarray | 337. House Robber III |
869. Reordered Power of 2 | 1593C - Save More Mice |